<?php

namespace App\Http\Controllers\Admin;

use App\Models\AdminUser;
use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class IndexController extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 首页
     */
    public function index()
    {
        return view('Admin.Index.index');
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 主页
     */
    public function home()
    {
        $todayApply = Order::where([['created_at', '>=', date('Y-m-d').' 00:00:00'],['status','=',0]])
            ->count();
        $todaySuccess = Order::where([['created_at', '>=', date('Y-m-d').' 00:00:00'],['status','=',1]])
            ->count();
        $totalApply = Order::where(['status'=>0])
            ->count();
        $totalSuccess = Order::where(['status'=>1])
            ->count();

        $arr = ['todayApply'=>$todayApply,'todaySuccess'=>$todaySuccess,'totalApply'=>$totalApply,'totalSuccess'=>$totalSuccess];

       /* $collection = collect($arr);
        $collection->dd();*/
        return view('Admin.Index.home',compact('arr'));
    }

    /**
     * @return array
     * 统计
     */
    public function statistics(){
        /***统计开始**/
        $BeginDate = [];//7天日期
        $standArr = [];//标准数据
        for($i=-12;$i<0;$i++) {
            $BeginDate[] = date('Y-m-d', strtotime($i.' days'));
            $standArr[date('Y-m-d', strtotime($i.' days'))] = 0;
        }
        //申请数目
        $list = DB::select('SELECT count(id) as counts, date_format(`created_at`,\'%Y-%m-%d\') sdate  FROM `orders` where status = 0 group by sdate ORDER BY sdate desc limit 12');
        $listArr = $this->setStand($list,$standArr);
        //成功数目
        $success = DB::select('SELECT count(id) as counts, date_format(`created_at`,\'%Y-%m-%d\') sdate  FROM `orders` where status = 1 group by sdate ORDER BY sdate desc limit 12');
        $successArr = $this->setStand($success,$standArr);
        //失败数目
        $fails = DB::select('SELECT count(id) as counts, date_format(`created_at`,\'%Y-%m-%d\') sdate  FROM `orders` where status = -1 group by sdate ORDER BY sdate desc limit 12');
        $failsArr = $this->setStand($fails,$standArr);
        //总数
        $totals = DB::select('SELECT count(id) as counts, date_format(`created_at`,\'%Y-%m-%d\') sdate  FROM `orders`  group by sdate ORDER BY sdate desc limit 12');
        $totalArr = $this->setStand($totals,$standArr);
        $statistics = ['date'=>$BeginDate,'list'=>$listArr,'success'=>$successArr,'fails'=>$failsArr,'total'=>$totalArr];
        /***统计结束**/
        return $statistics;
    }

    /**
     * @param $template
     * @param $standArr
     * @return array
     * 结果处理：
     */
    public function setStand($template,$standArr)
    {
        $successList = [];
        foreach($template as $item)
        {
            if(array_key_exists($item->sdate,$standArr)){
                $successList[$item->sdate] = $item->counts;
            }
        }
        $success = array_merge($standArr,$successList);
        return array_values($success);
    }
}
